package com.li.register;

import com.li.domain.Users;
import com.li.utils.DataSourceUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;

/**
 * @Author liyuefan
 * @Date ${Date}
 * Description
 */
@WebServlet(name = "Register")
public class Register extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        Users users = new Users();//bean
        Map<String, String[]> map = request.getParameterMap();//获取表单内容
        try {
            BeanUtils.populate(users, map);//将表单内容封装到bean中
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        users.setid(UUID.randomUUID() + "");//设置数据表id
        boolean b = check(users,runner,request,response);
        System.out.println(b);
        if (b == true){
            regist(users,runner);
        }
        response.sendRedirect(request.getContextPath());
    }

    private boolean check(Users users, QueryRunner runner,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//注册检验
        //用户名位数校验
        String checkusername = users.getUsername();
        //判空操作
        if(checkusername == null){
            request.getRequestDispatcher("regist.jsp").forward(request,response);
        }
        if(checkusername.length() > 20 || checkusername.length() ==0) {
            request.setAttribute("RegistInfo","用户名长度不规范。最多20个字符");
            request.getRequestDispatcher("regist.jsp").forward(request,response);
            return false;
        }
        //用户名重复校验
        String sql = "select * from user_info_test where USER_NAME = ?";
        Users checkusers  = null;
        try {
            checkusers = runner.query(sql, new BeanHandler<Users>(Users.class), checkusername);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (checkusers != null){
            request.setAttribute("RegistInfo","用户名已存在");
            request.getRequestDispatcher("regist.jsp").forward(request,response);
            return false;
        }
        //密码位数校验
        String checkpassword = users.getUpassword();
                //判空操作
        if(checkpassword == null){
            request.getRequestDispatcher("regist.jsp").forward(request,response);
        }
        if(checkpassword.length() > 16 || checkpassword.length() < 8){
            request.setAttribute("RegistInfo","密码应为8~16位");
            request.getRequestDispatcher("regist.jsp").forward(request,response);
            return false;
        }
        //密码一致校验
        String repassword = request.getParameter("reupassword");
        if(!repassword.equals(checkpassword)){
            request.setAttribute("RegistInfo","两次密码不一致");
            request.getRequestDispatcher("regist.jsp").forward(request,response);
            return false;
        }
        return true;
    }

    private void regist(Users users, QueryRunner runner){//注册的方法
        //操作数据库
        Object[] params = {users.getid(),users.getUsername(),users.getUpassword()};
        String sql = "insert into user_info_test values(?,?,?)";
        try {
            runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}